package com.apes.fn.scm.crossCompany.transfer.service;

import com.apes.fn.scm.crossCompany.transfer.model.CrossCompanyTransfer;
import com.apes.fn.scm.crossCompany.transfer.repository.CrossCompanyTransferRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.service.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.crossCompany.transfer.service
 * @ClassName: CrossCompanyTransferFlowVoucherService
 * @Author: pyb
 * @CreateTime: 2020-07-01 18:10
 * @Description: 跨公司调拨产生凭证
 * @Version: 1.0
 */
@Service("crossCompanyTransferFlowVoucherService")
public class CrossCompanyTransferFlowVoucherService extends DomainService {
    CrossCompanyTransferRepository crossCompanyTransferRepository;

    @Autowired
    private void setCrossCompanyTransferRepository(CrossCompanyTransferRepository crossCompanyTransferRepository) {
        this.crossCompanyTransferRepository = crossCompanyTransferRepository;
    }
    /**
     * 跨公司调拨流转返厂凭证
     *
     * @param request
     */
    private void flowPurchaseReturnVoucher(SimpleRequest request) {
        CrossCompanyTransfer transfer = request.getO(CrossCompanyTransfer.class);
        PartyRole supplier =SpringManager.getBean(PartyRoleRepository.class).findOne("A1001") ;
        //根据合作方式分组
        transfer.getCrossCompanyTransferItems().stream().collect(Collectors.groupingBy(
                (item)
                        -> item.getInventoryBatch().getCooperation())).forEach((
                (cooperation, internalHelpReturnApprovalItems) -> {
                    // 由于账目问题 在根据部门分组
                    internalHelpReturnApprovalItems.stream().collect(Collectors.groupingBy(
                            (crossCompanyTransferItem) -> crossCompanyTransferItem.getInventoryBatch().getDept()
                    )).forEach(((dept, crossCompanyTransferItems) ->{
                        //创建凭证
                        Voucher voucher = new Voucher();
                        // 主表信息
                        voucher.setCompany(transfer.getCompany());
                        voucher.setDept(dept);
                        voucher.setClerk(transfer.getApplyUid());
                        voucher.setPartner(supplier);
                        voucher.setBusinessType(BusinessType.PURCHASE_RETURN);
                        voucher.setInventoryMode(cooperation.getInventoryMode());
                        voucher.setAccountingMethod(cooperation.getAccountingMethod());
                        voucher.setScene(transfer.getScene());
                        voucher.setOrigin(transfer.getId());
                        voucher.setOriginBusinessType(transfer.getInnerBusinessType());
                        voucher.setSource(transfer.getId());
                        voucher.setSourceBusinessType(transfer.getInnerBusinessType());
                        voucher.setContract(crossCompanyTransferItems.stream().findFirst().get().getInventoryBatch().getContract());
                        // 退回标记
                        voucher.setReturned(true);
                        // 从表信息
                        voucher.setVoucherItems(crossCompanyTransferItems.stream().map(item -> {
                            VoucherItem voucherItem = new VoucherItem();
                            voucherItem.setProduct(item.getProduct());
                            voucherItem.setTax(item.getTax());
                            voucherItem.setOwner(transfer.getShipLocation().getCompany());
                            voucherItem.setUom(item.getUom());
                            voucherItem.setProductQty(-1.00 * item.getProductReturnQty());
                            voucherItem.setPriceUnit(item.getPriceUnit());
                            voucherItem.setOriginItemId(String.valueOf(item.getId()));
                            voucherItem.setSourceItemId(String.valueOf(item.getId()));
                            voucherItem.setInventoryBatch(item.getInventoryBatch());
                            return voucherItem;
                        }).collect(Collectors.toSet()));
                        //写入凭证
                        Voucher newVoucher = invoke("voucher.create", MapUtil.mapped(voucher));
                        if (newVoucher.getInventoryMode().equals("IM_OWN")) {
                            //生成账单
                            invoke("invoice.create", MapUtil.mapped(invoke("voucher.flow.invoice", MapUtil.mapped(newVoucher))));
                        }
                    }));
                }));
        if ("receipt".equals(transfer.getState())) {
            transfer.setState("done");
            crossCompanyTransferRepository.saveAndFlush(transfer);
        }
    }
}
